#include <QApplication>
#include <QTime>
#include <QDebug>
#include <QThread>
#include <QRegExp>
#include <QGSettings>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

class Thread : public QThread
{
public:
    Thread(QObject *parent = nullptr)
        : QThread(parent)
    {
    }

protected:
    void run()
    {
        while (1) {
            qDebug() << "sssssssssssss";
            QThread::sleep(1);
        }
    }
};
bool isValidIP(QString ip)
{
    QRegExp rx2("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");
    return rx2.exactMatch(ip);
}

const QByteArray ControlCenterSchmaId = "com.deepin.dde.control-center"; // 控制中心 schmaId
const QByteArray ControlCenterSchmaPath = "/com/deepin/dde/control-center/"; // 控制中心 schmaPath
int main(int argc, char **argv)
{
    QApplication a(argc, argv);
    // Thread t;
    // t.start();
    // QThread::sleep(10);
    // qInfo() << "2222";
    // qInfo() << isValidIP("1210.20.44.33");
    // qInfo() << isValidIP("110.20.44");
    // qInfo() << isValidIP("110.20.44.");
    // qInfo() << isValidIP(".20.44.33.22");
    // qInfo() << isValidIP("110.20.44.33");

    // if (QGSettings::isSchemaInstalled(ControlCenterSchmaId)) {
    //     QGSettings settings(ControlCenterSchmaId, ControlCenterSchmaPath);
    //     // 退域显示恢复默认  加域隐藏 设置 true
    //     // settings.set("auto-login-visable", true);
    //     // settings.set("nopasswd-login-visable", true);
    //     settings.reset("auto-login-visable");
    //     settings.reset("nopasswd-login-visable");
    // }

    QNetworkAccessManager manager;
    if (manager.networkAccessible() == QNetworkAccessManager::NotAccessible) {
        manager.setNetworkAccessible(QNetworkAccessManager::Accessible);
    }

    QNetworkRequest request;
    // request.setUrl(QUrl("https://aptly.uniontech.com/pkg/nonfree-eagle-1053/release-candidate/W3g4Nl0t5Lit6ZOB5L-h56eR5LiA5pyf5a6a5Yi26aG555uu6ZuG5oiQ5LuT5bqTMjAyMi0xMS0wNCAxODoyMTo1MQ/pool/non-free/u/udcp-client/signed_udcp_1.7.0.24-1_amd64.deb"));
    request.setUrl(QUrl("https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe"));
    // request.setSslConfiguration(getSSLConfigurationByUrl(url));
    // request.setRawHeader("version", "1.9");

    // // 设置请求的数据范围
    // const QString &rangeValue = QString("bytes=%1-%2").arg(leftRange).arg(rightRange);
    // request.setRawHeader("Range", rangeValue.toUtf8());
    QNetworkReply *reply = manager.sendCustomRequest(request, "GET");
    // qInfo() << "read:" << reply->readBufferSize() << reply->bytesAvailable();
    // reply->setReadBufferSize(1024);
    // qInfo() << "read:" << reply->readBufferSize() << reply->bytesAvailable();
    reply->setReadBufferSize(10 * 1024);
    QObject::connect(reply, &QNetworkReply::downloadProgress, [&](qint64 bytesReceived, qint64 bytesTotal) {
        qInfo() << reply->bytesAvailable() << "read:" << reply->readAll().size() << "bytesReceived:" << bytesReceived << "bytesTotal:" << bytesTotal;
    });

    return a.exec();
}